/*
 * GoodsPresenter.java
 * business
 *
 * Created by ChenTao on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.main.home;

import android.graphics.Bitmap;

import com.yidiandao.business.constant.Common;
import com.yidiandao.business.constant.Preferences;
import com.yidiandao.business.data.model.IHomeModel;
import com.yidiandao.business.data.model.IOrderModel;
import com.yidiandao.business.data.model.IUserModel;
import com.yidiandao.business.data.model.OrderModel;
import com.yidiandao.business.data.response.QueryFrameResponse;
import com.yidiandao.business.data.response.QueryHomeHeaderResponse;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.view.SuperLoadingLayout;
import com.yidiandao.business.utils.ImageLoader;
import com.yidiandao.business.utils.LogUtils;
import com.yidiandao.business.utils.PreferenceUtils;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

/**
 * 首页-商品-Presenter
 */
public class HomePresenter implements HomeContract.Presenter {

    private HomeContract.View mHomeView;

    private IHomeModel mHomeModel;

    private IUserModel mUserModel;

    private boolean isFragmentStop;

    private boolean isLoadingNewSecondKillData;

    private boolean isShowFrame;

    private Runnable timerTask = new Runnable() {
        @Override
        public void run() {
            if (isFragmentStop) {
                return;
            }
            mHomeView.autoHideFrameDialog();
        }
    };

    private List<QueryHomeHeaderResponse.Banner> mBannerList;
    private IOrderModel mOrderModel;

    public HomePresenter(HomeContract.View view, IHomeModel homeModel, IUserModel userModel) {
        mHomeView = view;
        mHomeModel = homeModel;
        mUserModel = userModel;
        mOrderModel = new OrderModel();

        mHomeView.setPresenter(this);
    }

    @Override
    public void start() {
        mUserModel.setUserIdForBugly();
        loadHomeData();
    }

    @Override
    public void loadHomeData() {
        mHomeModel.loadHomeData(
                mHomeView.getPagerTag(),
                new IHomeModel.OnLoadHomeHeaderListener() {
                    @Override
                    public void onBanner(
                            List<QueryHomeHeaderResponse.Banner> banners, List<String> bannerUrls) {
                        mBannerList = banners;
                        mHomeView.setBannerData(bannerUrls);
                    }

                    @Override
                    public void onSuccess(List<DisplayListItem> displayListItems) {
                        mHomeView.refreshContent(mHomeView.checkResultObject(displayListItems));
                        mHomeView.setDisplayListItems(displayListItems);
                        mHomeView.setRefreshComplete();
                    }

                    @Override
                    public void onFailed() {
                        mHomeView.setToolbarVisible();
                        mHomeView.refreshContent(SuperLoadingLayout.STATE_ERROR);
                    }
                });
    }

    @Override
    public List<QueryHomeHeaderResponse.Banner> getBannerList() {
        return mBannerList;
    }

    @Override
    public void getFrame() {
        mHomeModel.getFrame(
                mHomeView.getPagerTag(),
                new IHomeModel.OnFrameListener() {
                    @Override
                    public void onSuccess(final QueryFrameResponse.Frame frame) {

                        if (isFragmentStop) {
                            return;
                        }

                        String img = frame.getImg();
                        ImageLoader.downloadImage(
                                UIUtils.getContext(),
                                img,
                                new ImageLoader.OnImageLoadListener() {
                                    @Override
                                    public void onSuccess(Bitmap bitmap) {
                                        if (isFragmentStop) {
                                            return;
                                        }
                                        isShowFrame = true;
                                        mHomeView.showFrameDialog(bitmap, frame);
                                    }
                                });
                    }

                    @Override
                    public void onFinal() {
                        //加载超过14天未收货的信息
                        //                        if (PriceUtils.isLogin()) {
                        getUnGetGoodsFromNet(false);
                        //                        }
                    }
                });
    }

    @Override
    public void onFragmentCreate() {
        // 只加载一次活动对话框
        getFrame();
    }

    @Override
    public void startTimer() {
        UIUtils.postDelayed(timerTask, Common.FRAME_DELAY);
    }

    @Override
    public void onFragmentStop() {
        isFragmentStop = true;
        timerTask = null;
    }

    @Override
    public void onFragmentStart() {
        isFragmentStop = false;
    }

    @Override
    public void getNewSecondKillData() {

        if (isLoadingNewSecondKillData) {
            return;
        }
        isLoadingNewSecondKillData = true;

        LogUtils.d("加载最新的秒杀数据");

        mHomeModel.getSecondKillData(
                mHomeView.getPagerTag(),
                new IHomeModel.OnLoadSecondKillListener() {
                    @Override
                    public void onSuccess() {
                        // 刷新Ui
                        mHomeView.notifyDataSetChanged();
                    }

                    @Override
                    public void onFinal() {
                        isLoadingNewSecondKillData = false;
                    }
                });
    }

    @Override
    public void checkIsSign() {
        mUserModel.checkIsSign(
                mHomeView.getPagerTag(),
                new IUserModel.OnCheckSignListener() {
                    @Override
                    public void onSuccess(boolean visible) {
                        mHomeView.setSignTipVisible(visible);
                    }
                });
    }

    @Override
    public void reCheckSign() {
        if (!PreferenceUtils.getBoolean(UIUtils.getContext(), Preferences.IS_OPEN_SIGN_TIP, true,
                false)) {
            mHomeView.setSignTipVisible(false);
            return;
        }
        if (PreferenceUtils.getToken(UIUtils.getContext()).equals(PreferenceUtils.STRING_DEFAULT)) {
            mHomeView.setSignTipVisible(true);
        } else {
            checkIsSign();
        }
    }

    @Override
    public void getUnGetGoodsFromNet(final boolean showDialogDirective) {
        mOrderModel.getUnGetGoodsCount(mHomeView.getPagerTag(),
                new IOrderModel.OnLoadUnGetGoodsListener() {
                    @Override
                    public void onSuccess(int count) {
                        mHomeView.setUnGetGoodsCount(count);
                        if ((count > 0 && showDialogDirective) || (count > 0 && !isShowFrame)) {
                            mHomeView.showUnGetGoodsTipDialog(count);
                        }
                    }
                });
    }
}
